<template>
    <div >
        <h1>{{id?"编辑":"新建"}}英雄</h1>
        <el-form label-width="80px" @submit.native.prevent = "save">
            <el-tabs type="border-card" value="skills">
                <el-tab-pane label="基本信息" >
                    <el-form-item label="名称">
                        <el-input v-model="model.name"></el-input>
                    </el-form-item>
                    <el-form-item label="称号">
                        <el-input v-model="model.alias"></el-input>
                    </el-form-item>
                    <el-form-item label="头像">
                        <el-upload
                         class="avatar-uploader"
                         :action="UploadUrl"
                         :headers="authHeaderMixin"
                         :show-file-list="false"
                         :on-success="res=>$set(model,'avatar',res.url)">
                            <img v-if="model.avatar" :src="model.avatar" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="背景图">
                        <el-upload
                         class="avatar-uploader"
                         :action="UploadUrl"
                         :headers="authHeaderMixin"
                         :show-file-list="false"
                         :on-success="res=>$set(model,'banner',res.url)">
                            <img v-if="model.banner" :src="model.banner" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>

                    <el-form-item label="类型">
                        <el-select v-model="model.categories" placeholder="请选择" multiple>
                            <el-option
                            v-for="item in categories"
                            :key="item._id"
                            :label="item.name"
                            :value="item._id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="难度">
                        <el-rate style="margin-top:0.6rem;" 
                         v-model="model.scores.diffcult"
                         :max=10 allow-half show-score
                         ></el-rate>
                    </el-form-item>
                    <el-form-item label="技能">
                        <el-rate style="margin-top:0.6rem;" 
                         v-model="model.scores.skill"
                         :max=10 allow-half show-score
                         ></el-rate>
                    </el-form-item>
                    <el-form-item label="攻击">
                        <el-rate style="margin-top:0.6rem;" 
                         v-model="model.scores.battle"
                         :max=10 allow-half show-score
                         ></el-rate>
                    </el-form-item>
                    <el-form-item label="生存">
                        <el-rate style="margin-top:0.6rem;" 
                         v-model="model.scores.survival"
                         :max=10 allow-half show-score
                         ></el-rate>
                    </el-form-item>
                    <el-form-item label="顺风出装">
                        <el-select v-model="model.equips.easy" placeholder="请选择" multiple>
                            <el-option
                            v-for="item in items"
                            :key="item._id"
                            :label="item.name"
                            :value="item._id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="逆风出装">
                        <el-select v-model="model.equips.hard" placeholder="请选择" multiple>
                            <el-option
                            v-for="item in items"
                            :key="item._id"
                            :label="item.name"
                            :value="item._id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="使用技巧">
                        <el-input type="textarea" v-model="model.usageTips"></el-input>
                    </el-form-item>
                    <el-form-item label="对战技巧">
                        <el-input type="textarea" v-model="model.battleTips"></el-input>
                    </el-form-item>
                    <el-form-item label="团战思路">
                        <el-input type="textarea" v-model="model.teamTips"></el-input>
                    </el-form-item>
                </el-tab-pane>
                <el-tab-pane label="技能信息" name="skills">
                    <el-button icon="el-icon-plus" type="text" @click="model.skills.push({})">添加技能</el-button>
                    <el-row type="flex" style="flex-wrap:wrap;">
                        <el-col :md="6" v-for="(item,index) in model.skills" :key="index">
                            <el-form-item label="技能名称">
                                <el-input v-model="item.name"></el-input>
                            </el-form-item>
                            <el-form-item label="技能图标">
                                <el-upload
                                 class="avatar-uploader"
                                 :action="UploadUrl"
                                 :headers="authHeaderMixin"
                                 :show-file-list="false"
                                 :on-success="res=>$set(item,'icon',res.url)">
                                    <img v-if="item.icon" :src="item.icon" class="avatar">
                                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                </el-upload>
                            </el-form-item>
                            <el-form-item label="冷却值">
                                <el-input v-model="item.cd"></el-input>
                            </el-form-item>
                            <el-form-item label="消耗">
                                <el-input v-model="item.cost"></el-input>
                            </el-form-item>
                            <el-form-item label="技能介绍">
                                <el-input type="textarea" v-model="item.info"></el-input>
                            </el-form-item>
                            <el-form-item label="小提示">
                                <el-input type="textarea" v-model="item.tips"></el-input>
                            </el-form-item>
                            <el-form-item >
                                <el-button type="danger" @click="model.skills.splice(index,1)">删除</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row> 
                </el-tab-pane>
                <el-tab-pane label="最佳搭档" name="partner">
                    <el-button icon="el-icon-plus" type="text" @click="model.relations.partner.push({})">添加搭档</el-button>
                    <el-row type="flex" style="flex-wrap:wrap;">
                        <el-col :md="6" v-for="(item,index) in model.relations.partner" :key="index">
                            <el-form-item label="英雄名称">
                                <el-select v-model="item.hero" filterable>
                                    <el-option
                                    v-for="hero in heroes" :key="hero._id"
                                    :value="hero._id"
                                    :label="hero.name"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="关系介绍">
                                <el-input type="textarea" v-model="item.tip"></el-input>
                            </el-form-item>
                            <el-form-item >
                                <el-button type="danger" @click="model.relations.partner.splice(index,1)">删除</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row> 
                </el-tab-pane>
            </el-tabs>
            <el-form-item style="margin-top:1rem;">
                <el-button type="primary" native-type="submit">确认</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        props: {
            id:String
        },
        data () {
            return {
                heroes:[],
                categories:[],
                items:[],
                model:{
                    name:'',
                    avatar:'',
                    categories:[],
                    alias: "",
                    scores:{},
                    equips:{
                        easy:[],
                        hard:[]
                    },
                    skills:[],
                    relations:{
                        partner:[]
                    }
                },
            }
        },
        methods: {
            uploaded(res){
                // this.$set(this.model,'avatar',res.url)
                this.model.avatar = res.url
            },
            async save(){
                let res
                if(this.id){
                    res = await this.$http.put(`rest/heroes/${this.id}`,this.model)
                }else{
                    res = await this.$http.post("rest/heroes",this.model)
                }
                
                if(res){
                    this.$router.push('/heroes/list')
                    this.$message({
                        type:'success',
                        message:'保存成功'
                    })
                }
            },
            async fetch(){
                const res = await this.$http.get(`rest/heroes/${this.id}`)
                if(res){
                    this.model = {...this.model,...res.data}
                }
            },
            async fetchCategories(){
                const res = await this.$http.get(`rest/categories`)
                if(res){
                    this.categories = res.data
                }
            },
            async fetchHeroes(){
                const res = await this.$http.get(`rest/heroes`)
                if(res){
                    this.heroes = res.data
                }
            },
            async fetchEquips(){
                const res = await this.$http.get(`rest/items`)
                if(res){
                    this.items = res.data
                }
            },
        },
        beforeRouteEnter (to, from, next) {
            next(vm=>{
                if(vm.id){
                    vm.fetch()
                }else{
                    vm.model={
                    name:'',
                    avatar:'',
                    categories:[],
                    alias: "",
                    scores:{},
                    equips:{
                        easy:[],
                        hard:[]
                    },
                    skills:[],
                    }
                }
                vm.fetchCategories()
                vm.fetchHeroes()
                vm.fetchEquips()
            })
        },

    }
</script>

<style>
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    min-width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    min-width: 178px;
    height: 178px;
    display: block;
  }
</style>